/**
 * PaymentLogServiceImp.java
 * 
 * Copyright@2015 OVT Inc. All rights reserved. 
 * 
 * 2015年12月22日
 */
package com.ovt.order.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ovt.common.exception.OVTRuntimeException;
import com.ovt.common.utils.CollectionUtils;
import com.ovt.order.dao.AliPaymentLogDaoImp;
import com.ovt.order.dao.contant.DBConstants.TABLES.ALI_PAYMENT_LOG;
import com.ovt.order.dao.vo.AliPaymentLog;
import com.ovt.order.dao.vo.PaymentLog;
import com.ovt.order.service.exception.ServiceErrorCode;
import com.ovt.order.service.exception.ServiceException;

/**
 * PaymentLogServiceImp
 * 
 * @Author leo.liu
 * @Version 1.0
 * @See
 * @Since [OVT Cloud Platform]/[API] 1.0
 */
@Service
public class PaymentLogServiceImp implements PaymentLogService
{
    @Autowired
    private AliPaymentLogDaoImp aliPaymentLogDao;

    /*
     * (non-Javadoc)
     * 
     * @see com.ovt.service.PaymentLogService#saveAliPaymentLog(java.util.Map)
     */
    @Override
    public AliPaymentLog saveAliPaymentLog(Map<String, String> params)
            throws ServiceException
    {
        AliPaymentLog aliPaymentLog = new AliPaymentLog();
        aliPaymentLog.setNotifyTime(Timestamp.valueOf(params
                .get(ALI_PAYMENT_LOG.NOTIFY_TIME)));
        aliPaymentLog.setNotifyType(params.get(ALI_PAYMENT_LOG.NOTIFY_TYPE));
        aliPaymentLog.setNotifyId(params.get(ALI_PAYMENT_LOG.NOTIFY_ID));
        aliPaymentLog.setSignType(params.get(ALI_PAYMENT_LOG.SIGN_TYPE));
        aliPaymentLog.setSign(params.get(ALI_PAYMENT_LOG.SIGN));
        aliPaymentLog.setOutTradeNo(params.get(ALI_PAYMENT_LOG.OUT_TRADE_NO));
        aliPaymentLog.setSubject(params.get(ALI_PAYMENT_LOG.SUBJECT));
        aliPaymentLog.setTradeNo(params.get(ALI_PAYMENT_LOG.TRADE_NO));
        aliPaymentLog.setTradeStatus(params.get(ALI_PAYMENT_LOG.TRADE_STATUS));
        aliPaymentLog.setBuyerId(params.get(ALI_PAYMENT_LOG.BUYER_ID));
        aliPaymentLog.setBuyerEmail(params.get(ALI_PAYMENT_LOG.BUYER_EMAIL));
        aliPaymentLog.setTotalFee(Float.valueOf(params
                .get(ALI_PAYMENT_LOG.TOTAL_FEE)));
        aliPaymentLog.setGmtCreate(Timestamp.valueOf(params
                .get(ALI_PAYMENT_LOG.GMT_CREATE)));
        aliPaymentLog.setGmtPayment(Timestamp.valueOf(params
                .get(ALI_PAYMENT_LOG.GMT_PAYMENT)));
        aliPaymentLog.setIsTotalFeeAdjust(params.get(
                ALI_PAYMENT_LOG.IS_TOTAL_FEE_ADJUST).toCharArray()[0]);
        aliPaymentLog.setUseCoupon(params.get(ALI_PAYMENT_LOG.USE_COUPON)
                .toCharArray()[0]);
        aliPaymentLog.setDiscount(Float.valueOf(params
                .get(ALI_PAYMENT_LOG.DISCOUNT)));
        aliPaymentLog
                .setRefundStatus(params.get(ALI_PAYMENT_LOG.REFUND_STATUS));
        aliPaymentLog.setGmtRefund(Timestamp.valueOf(params
                .get(ALI_PAYMENT_LOG.GMT_REFUND)));

        try
        {
            long id = aliPaymentLogDao.save(aliPaymentLog);
            aliPaymentLog.setId(id);
        }
        catch (OVTRuntimeException e)
        {
            throw new ServiceException(ServiceErrorCode.DB_SERVER_ERROR,
                    "Failed to save alipay payment log!", e);
        }

        return aliPaymentLog;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ovt.service.PaymentLogService#getAliPaymentLogList()
     */
    @Override
    public List<AliPaymentLog> getAliPaymentLogList() throws ServiceException
    {
        List<AliPaymentLog> aliPaymentLogList = new ArrayList<AliPaymentLog>();

        List<PaymentLog> paymentLogList = null;
        try
        {
            paymentLogList = aliPaymentLogDao.getPaymentLogList();
        }
        catch (OVTRuntimeException e)
        {
            throw new ServiceException(ServiceErrorCode.DB_SERVER_ERROR,
                    "Failed to get alipay payment log list!", e);
        }

        if (CollectionUtils.isNotEmpty(paymentLogList))
        {
            for (PaymentLog log : paymentLogList)
            {
                aliPaymentLogList.add((AliPaymentLog) log);
            }
        }

        return aliPaymentLogList;
    }
}
